$.ajax({
  type: 'get',
  url: '/comments',
  success: function (response) {
    const html = template('commentsTpl', response)
    $('#commentsBox').html(html);
    const pagehtml = template('pageTpl', response);
    $('#pageBox').html(pagehtml);
  }
})

// 实现分页
function changePage(page) {
  $.ajax({
    type: 'get',
    url: '/comments',
    data: {
      page
    },
    success: function (response) {
      const html = template('commentsTpl', response)
      $('#commentsBox').html(html);
      const pagehtml = template('pageTpl', response);
      $('#pageBox').html(pagehtml);
    }
  })
}

// 审核状态
$('#commentsBox').on('click', '.state', function () {
  const state = $(this).attr('data-state');
  const id = $(this).attr('data-id');
  $.ajax({
    type: 'put',
    url: '/comments/' + id,
    data: {
      state: (1 - state)
    },
    success: function () {
      location.reload();
    }
  })
})

// 删除操作
$('#commentsBox').on('click', '.delete', function () {
  if(confirm('您确认要删除该评论吗')) {
    const id = $(this).attr('data-id');
    $.ajax({
      type: 'delete',
      url: '/comments/' + id,
      success: function () {
        location.reload();
      }
    })
  }
})